<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@ include file="/WEB-INF/taglibs.jsp"%>
<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>
<script type="text/javascript" src="${ctx}/resource/system/common/js/ajaxfileupload.js"></script>
<script type="text/javascript">
	$(function(){
		$("#serveMenu").addClass("cur");
	});
</script>

<div class="conTit fixed">
    <p>
   		<c:choose>
			<c:when test="${info.id ==null}">新增活动</c:when>
			<c:otherwise>修改活动</c:otherwise>
		</c:choose>
    </p>
    <div class="fr"><a href="javascript:void(0);" class="backBtn" onclick="window.history.back(-1);">返回</a></div>
</div>

<div style="margin-left: 10px;">
  <form id="carrierForm" action="" method="post">
	<div class="inputArea">
	    <label>描述:<font style="color: red">*</font></label>
	     <div class="inputLine">
	          <input type="text" id="activityName" name="activityName" value="${info.activityName}" />
	     </div>
	</div> 
	<div class="inputArea">
	    <label>描述:<font style="color: red">*</font></label>
	     <div class="inputLine">
	          <input type="text" id="description" name="description" value="${info.description}" />
	     </div>
	</div> 
	<div class="inputArea">
	    <label>链接:<font style="color: red">*</font></label>
	     <div class="inputLine">
	          <input type="text" id="linkUrk" name="linkUrl" value="${info.linkUrl}" />
	     </div>
	</div>
		<div class="inputArea02">
	  <label>图片: 建议尺寸：<span>640*320</span> <font style="color: red">*</font></label>
	    <input type="hidden" id="imageUrl" name="imageUrl" value="${info.imageUrl}"/>
	    <div class="inputLine">
	    	   上传轮播图<input id="uploadfile" name="cmfile" type="file" onchange="triggerUpload()" />
	    </div>
	    <div id="imgPreView" >
		        <div id="preview" >
			      	 <div id="thumb" style="margin-left:21%; margin-top:30px;">
			      	 		<c:if test="${info.imageUrl !=null && info.imageUrl!='' }">
			      				<img style="width:460px;" src="${imgPath}/${info.imageUrl}"   />
					    	</c:if>
					 </div>
				</div>
		  </div>
	</div>
 
	<div class="save">
	     <a href="javascript:void(0);" class="backBtn" onclick="saveCarrier()">保存</a>
	</div>
    <input type="hidden" name="id" value="${info.id}" />
  </form>
</div>
		
<script type="text/javascript">

function saveCarrier() {
	var description = $("#description").val();
	var activityName = $("#activityName").val();
	var linkUrk = $("#linkUrk").val();
	var imageUrl = $("#imageUrl").val();
	
	
	if($.trim(description) ==""){
		alert("描述不能为空");
		success = false;
	}else if($.trim(activityName) ==""){
		alert("内容不能为空");
		success = false;
	}
	else if($.trim(linkUrk) ==""){
		alert("链接不能为空");
		success = false;
	}
	else if($.trim(activityName) ==""){
		alert("请上传图片");
		success = false;
	}

	if(success){
		
		$.ajax({
			url:'${ctx}/admin/activity/save.json',
			type: "post",
			data:{id:id,description:description,activityName:activityName,linkUrl:linkUrl,imageUrl:imageUrl},
			dataType: 'json',//返回值类型 一般设置为json
			success: function (data, status) {
				if(data.status==true || data.status=="true") {
					alert("保存成功");
					window.location.reload();
				}else {
					layer.alert(data.msg);
				}
			}
		});
	}
	
	 function triggerUpload(){
   	  $("#imgPreView").show();
   	  $("#thumb").html('<img src="${ctx}/resource/system/admin/images/loading.gif" width="25" style="display:inline-block;float:left;margin:0 5px 0 0;">'+'<p style="line-height:25px;float:none;color:#78A200;font-size:12px;">文件上传中,请耐心等待...</p>');
		  var fileName = $("#uploadfile").val().toLowerCase();
		  if(fileName.indexOf(".png")>=0 || fileName.indexOf(".jpg")>=0 || fileName.indexOf(".gif")>=0) {
			$.ajaxFileUpload({
				url:'${ctx}/admin/activity/uploadActivity.json',//用于文件上传的服务器端请求地址
				secureuri:false, //是否需要安全协议，一般设置为false
				fileElementId:'uploadfile',  //文件上传域的ID
				data:{fsize:5},
				dataType: 'json',//返回值类型 一般设置为json
				success: function (data, status) {
					//console.log(data);
					$("#thumb").empty();
					if(data.status==true || data.status=="true") {
						$("#pictureUrl").val(data.data.filePath);
						$("#thumb").html('<img style="max-height:320px;max-width: 640px;" src="${imgPath}/'+data.data.filePath+'"/>');
					}else {
						$("#thumb").html(data.msg);
					}
				},
				 error: function (data, status, e) {
					console.log(data);
					$("#thumb").empty();
					alert("上传失败，请重新尝试。");
				}
			});
			} else {
				$("#thumb").empty();
				alert("请上传正确格式的图片！");
			}	
	 }
}
</script>
